#!/system/bin/sh

dir=splitsh
adir=AUTO

list=$dir/$adir/00_list.txt

if="${1:-blob}" # main.1200.com.gameloft.android.ANMP.GloftPOHM.obb}
off=`dd "if=$if" bs=9 count=1 2>/dev/null`

function sub () { # name for auto-dir
	test "${base%??}" = "rk" && base=animate && return
	case "${base##*.}" in
mpc|wav)					base=sound;;
xml|xib|bgeom)				base=xml;;
trail|particle|csv|anim)	base=animate;;
fnt|loc)					base=locales;;
png|sprite|pvr|rkm|rk)		base=gui;;
*) base=;;
	esac
}

function unp () {
	sz=$(( $2 - $1 ))
	file="$5"
	base="${5%/*}"
	test "$base" = "$5" && sub && test -n "$base" && base="$adir/$base" && file="$base/$file"
	sub="$dir/$base"
	test -n "$base" -a ! -d "$sub" && mkdir "$sub" # && echo "mkdir $sub"
	printf '%05u %s\n' "$3" "$5" >> "$list"
	dd if="$if" of="$dir/$file" skip="$1" ibs=1 count="$sz" 2> /dev/null
}

dd "if=$if" ibs=1 skip=$off | { mkdir -p "$dir/$adir"; while read IN; do unp $IN; done; }
